package com.bawei.monthdemo2207a.month02.upload

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bawei.monthdemo2207a.R
import com.blankj.utilcode.util.FileUtils
import kotlinx.coroutines.launch
import java.io.File

/**
 * 作者：赵亮
 * 时间：2024/12/31 16:57
 * 功能：
 */
@Composable
fun UploadPage() {

    val list = remember {
        SnapshotStateList<File>()
    }

    LaunchedEffect(key1 = "") {
        launch {
            list.addAll(File("/sdcard/Download").listFiles())
        }
    }

    var cbName by remember {
        mutableStateOf("")
    }


    var selectedTabIndex by remember {
        mutableStateOf(0)
    }

    Column(modifier = Modifier.padding(top = 24.dp)) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 14.dp), horizontalArrangement = Arrangement.SpaceBetween
        ) {
            listOf("全选", "编辑", "取消").forEachIndexed { index, s ->
                if (index == 1) {
                    Text(text = s, fontSize = 22.sp)
                } else {
                    Text(text = s, fontSize = 15.sp)
                }
            }
        }
        TabRow(divider = {}, selectedTabIndex = selectedTabIndex) {
            listOf("全部", "DOC", "XLS", "TXT", "PPT").forEachIndexed { index, s ->
                Tab(selected = selectedTabIndex == index, onClick = { selectedTabIndex = index }) {
                    Text(text = s, fontSize = 15.sp)
                }
            }
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 15.dp)
                .background(
                    color = colorResource(id = R.color.purple_200),
                    shape = RoundedCornerShape(10.dp)
                )
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(40.dp)
                    .padding(horizontal = 20.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(imageVector = Icons.Default.Search, contentDescription = null)
                BasicTextField(value = "搜索文件", onValueChange = {})
            }
        }

        // 文件列表
        LazyColumn(modifier = Modifier.weight(1f)) {
            items(list) {
                Row(modifier = Modifier.fillMaxWidth()) {
                    Image(
                        painter = painterResource(id = R.drawable.logo),
                        contentDescription = null,
                        modifier = Modifier.size(40.dp)
                    )
                    Column(modifier = Modifier.weight(1f)) {
                        Text(text = it.name)
                        Text(text = FileUtils.getSize(it))
                    }
                    RadioButton(selected = cbName == it.name, onClick = { cbName = it.name })
                }
            }
        }

        // 底部操作菜单
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 10.dp)
        ) {
            listOf("移动到...", "复制", "删除", "更多").forEachIndexed { index, s ->
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Icon(imageVector = Icons.Default.Delete, contentDescription = null)
                    Text(text = s)
                }
            }
        }

    }
}

@Preview
@Composable
fun UploadPagePreview() {
    UploadPage()
}

